 /*
  * Copyright (c) 2021 Huawei Device Co., Ltd.
  * <p>
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * <p>
  * http://www.apache.org/licenses/LICENSE-2.0
  * <p>
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package org.angmarc.views;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Text;
import ohos.app.Context;
import org.angmarc.app.ResourceTable;
import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;
import org.angmarch.views.PopUpTextAlignment;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class NiceSpinnerTest {

    private NiceSpinner niceSpinner;

    @Before
    public void setUp() throws Exception {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        List<String> list = new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        niceSpinner = new NiceSpinner(context);
        niceSpinner.attachDataSource(list);
    }

    @After
    public void tearDown() throws Exception {
        niceSpinner = null;
    }

    @Test
    public void getItemAtPosition() {
        Assert.assertEquals("four", niceSpinner.getItemAtPosition(3));
    }

    @Test
    public void getSelectedItem() {
        niceSpinner.setSelectedIndex(2);
        Assert.assertEquals("three", niceSpinner.getSelectedItem());
    }

    @Test
    public void getSelectedIndex() {
        niceSpinner.setSelectedIndex(2);
        Assert.assertEquals(2, niceSpinner.getSelectedIndex());
    }

    @Test
    public void setSelectedIndex() {
        niceSpinner.setSelectedIndex(2);
        Assert.assertEquals(2, niceSpinner.getSelectedIndex());
    }

    @Test
    public void attachDataSource() {
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        niceSpinner.attachDataSource(list);
        Assert.assertEquals("hello", niceSpinner.getItemAtPosition(0));
    }

    @Test
    public void setAdapter() {
        List<String> list = new ArrayList<>();
        list.add("hello");
        list.add("world");
        MyProvider myProvider = new MyProvider(list);
        niceSpinner.setAdapter(myProvider);
        Assert.assertEquals("world", niceSpinner.getItemAtPosition(1));
    }

    @Test
    public void getPopUpTextAlignment() {
        niceSpinner.setPopupTextAliment(PopUpTextAlignment.CENTER);
        PopUpTextAlignment popUpTextAlignment = niceSpinner.getPopUpTextAlignment();
        Assert.assertEquals(2, popUpTextAlignment.ordinal());
    }

    @Test
    public void hideArrow() {
        niceSpinner.hideArrow();
        Assert.assertTrue(niceSpinner.isArrowHidden());
    }

    @Test
    public void showArrow() {
        niceSpinner.showArrow();
        Assert.assertFalse(niceSpinner.isArrowHidden());
    }

    @Test
    public void isArrowHidden() {
        niceSpinner.hideArrow();
        Assert.assertTrue(niceSpinner.isArrowHidden());
    }

    @Test
    public void setDropDownListPaddingBottom() {
        niceSpinner.setDropDownListPaddingBottom(20);
        Assert.assertEquals(20, niceSpinner.getDropDownListPaddingBottom());
    }

    @Test
    public void getDropDownListPaddingBottom() {
        niceSpinner.setDropDownListPaddingBottom(20);
        Assert.assertEquals(20, niceSpinner.getDropDownListPaddingBottom());
    }

    @Test
    public void performItemClick() {
        niceSpinner.performItemClick(1, false);
        Assert.assertEquals(1, niceSpinner.getSelectedIndex());
    }

    @Test
    public void getOnSpinnerItemSelectedListener() {
        niceSpinner.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener() {
            @Override
            public void onItemSelected(NiceSpinner parent, Component view, int position, long id) {
            }
        });
        Assert.assertNotNull(niceSpinner.getOnSpinnerItemSelectedListener());
    }

    @Test
    public void setPopupTextAliment() {
        niceSpinner.setPopupTextAliment(PopUpTextAlignment.CENTER);
        Assert.assertEquals(2, niceSpinner.getPopUpTextAlignment().ordinal());
    }

    @Test
    public void setStrArrayData() {
        niceSpinner.setStrArrayData(ResourceTable.Strarray_courses);
        Assert.assertEquals("Chinese", niceSpinner.getItemAtPosition(1));
    }

    public static class MyProvider extends BaseItemProvider {

        List<String> data;

        public MyProvider(List<String> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            return data == null ? 0 : data.size();
        }

        @Override
        public Object getItem(int i) {
            return data.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
            return new Text(AbilityDelegatorRegistry.getAbilityDelegator().getAppContext());
        }
    }
}